package com.dc.service.impl;

import com.dc.dao.UserDao;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.User;
import com.dc.service.UserService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
//    分页查询用户
    public BaseModel selectPageUser(Integer curr_page, Integer page_size,BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<User> user = userDao.selectPageUser();
        PageInfo pageInfo = new PageInfo(user,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询用户成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }
//新增用户
    public BaseModel addUser(User user, BaseModel baseModel) throws Exception{
        int countUser = userDao.selectByAccount(user.getAccount());
        if (countUser > 0){
//            有账号不能插入
            throw new BusinessException(ErrorMessageEnum.REPEAT_ACCOUNT);
        }
        Integer result = userDao.insertUser(user);
        if(result <=0){
            throw new BusinessException(ErrorMessageEnum.ADD_USER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增成功");
        return baseModel;
    }
//  登录
    public BaseModel login(String account, String password, BaseModel baseModel) throws Exception {
        User user = userDao.selectByAccountAndPwd(account,password);
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("登录成功");
        user.setPassword(null);
        baseModel.setData(user);
        return baseModel;
    }
//    修改用户
    public BaseModel updateUser(User user, BaseModel baseModel) throws Exception {
//        用户无法修改账号
        user.setAccount(null);
        int result = userDao.updateUser(user);
        if(result <=0){
            throw new BusinessException(ErrorMessageEnum.UPDATE_USER);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改成功");
        return baseModel;
    }
//    删除用户
    public BaseModel deletedUser(String ids, BaseModel baseModel) throws Exception {
        String[] idArry = ids.split(",");
        int result = userDao.deletedUser(idArry);
        if(result <= 0){
            throw new BusinessException(ErrorMessageEnum.DELETED_USER);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result +"条记录成功");
        return baseModel;
    }
//按姓名查用户信息
    public BaseModel selectPageByName(String name, Integer curr_page, Integer page_size, BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<User> user = userDao.selectUserByName(name);
        PageInfo pageInfo = new PageInfo(user,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询用户成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }
//根据id查询用户
    public BaseModel selectUserById(int id, BaseModel baseModel) throws Exception {
        User user = userDao.selectUserById(id);
        if(user == null) {
            throw new BusinessException(ErrorMessageEnum.ADD_USER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("查询用户成功");
        baseModel.setData(user);
        return baseModel;
    }
//修改用户状态
    public BaseModel updateUserState(int id, int state, BaseModel baseModel) throws Exception {
        int result = userDao.updateUserState(id,state);
        if(result <=0 ) {
            throw new BusinessException(ErrorMessageEnum.UPDATE_USER);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改成功");
        return baseModel;
    }
//手机验证登录
    public BaseModel codeLogin(String phone, BaseModel baseModel) throws Exception {
        User user = userDao.selectUserByAccount(phone);
        System.out.println(user);
        if(user == null){
            throw new BusinessException(ErrorMessageEnum.LOGIN_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("登录成功");
        user.setPassword(null);
        baseModel.setData(user);
        return baseModel;
    }
}
